package com.gxsyh.nantie.refund.management.provider.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxsyh.nantie.commons.configure.rabbitmq.RabbitMQConfig;
import com.gxsyh.nantie.commons.dto.PageHelpDTO;
import com.gxsyh.nantie.commons.dto.ResponseResult;
import com.gxsyh.nantie.commons.dto.SendLogResult;
import com.gxsyh.nantie.commons.utils.OkHttpClientUtil;
import com.gxsyh.nantie.refund.management.provider.controller.dto.RefundConfirmParam;
import com.gxsyh.nantie.refund.management.provider.domain.ApartmentRefundList;
import com.gxsyh.nantie.refund.management.provider.domain.UserRefundInfo;
import com.gxsyh.nantie.refund.management.provider.service.ApartmentRefundListService;
import com.gxsyh.nantie.refund.management.provider.service.TbRefundService;
import com.gxsyh.nantie.refund.management.provider.service.UserRefundInfoService;
import okhttp3.ResponseBody;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class ApartmentRefundController {

    @Resource
    private ApartmentRefundListService apartmentRefundListService;

    @Resource
    private UserRefundInfoService userRefundInfoService;

    @Resource
    private TbRefundService tbRefundService;

    @Value("${base.config.leaf.url}")
    String LEAF_URL;

    @Resource
    AmqpTemplate amqpTemplate;


    /**
     * 获取 公寓退款管理 列表
     * @param pageNum
     * @return
     */
    @GetMapping(value = "/refund/apartment/list/{pageNum}")
    public ResponseResult<PageHelpDTO> userList(@PathVariable int pageNum,@RequestParam Long payDepartmentId){
        PageHelper.startPage(pageNum,5,"tb_refund.createTime DESC");
        PageInfo<ApartmentRefundList> pageInfo=new PageInfo<>(apartmentRefundListService.selectApartmentRefund(payDepartmentId));
        List<ApartmentRefundList> apartmentRefundLists = pageInfo.getList();
        PageHelpDTO pageHelpDTO=new PageHelpDTO();
        pageHelpDTO.setPages(pageInfo.getPages());
        pageHelpDTO.setTotal(pageInfo.getTotal());
        pageHelpDTO.setData(apartmentRefundLists);
        return new ResponseResult<>(200,"公寓退款列表",pageHelpDTO);
    }

    /**
     * 获取退款详情
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/refund/apartment/list/info")
    public ResponseResult<UserRefundInfo> refundInfo(@RequestParam long refundId) throws Exception {
        UserRefundInfo refundInfo =userRefundInfoService.byRefundIdGetInfo(refundId);
        return new ResponseResult<>(HttpStatus.OK.value(),"获取退款详情成功",refundInfo);
    }

    /**
     * 修改退款状态为 1  即公寓审核通过
     * @return
     * @throws
     */
    @PostMapping(value = "/refund/apartment/list/confirm")
    public ResponseResult<Void> refundconfirm(@RequestBody RefundConfirmParam refundConfirmParam, HttpServletResponse httpServletResponse) throws Exception {

        int i =  tbRefundService.updateTbUserRefundStatus(refundConfirmParam.getRefundId(),1);
        if (i>0)
        {
            //                使用消息队列发送日志
            SendLogResult sendLogResult = new SendLogResult();
            sendLogResult.setUsername(refundConfirmParam.getRefundInfo());
            sendLogResult.setInfo("向退款ID:"+refundConfirmParam.getRefundId()+"修改状态为:"+1+"-公寓确认完毕");
            amqpTemplate.convertAndSend(RabbitMQConfig.SEND_LOG_QUEUE,sendLogResult);
//                end消息队列发送日志

            return new ResponseResult<>(HttpStatus.OK.value(),"公寓确认完毕",null);
        }
        else {
            httpServletResponse.setStatus(500);
            return new ResponseResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(),"请检查输入格式");
        }
    }

    /**
     * 退修改退款状态为 3：不通过并填写备注
     * @return
     * @throws
     */
    @PostMapping(value = "/refund/apartment/list/veto")
    public ResponseResult<Void> refundVeto(@RequestBody RefundConfirmParam refundConfirmParam, HttpServletResponse httpServletResponse) throws Exception {

        int i =tbRefundService.updateTbUserRefundInfo(refundConfirmParam.getRefundId(),"公寓："+refundConfirmParam.getRefundInfo(),3);
        if (refundConfirmParam.getRefundInfo().isEmpty()){
            httpServletResponse.setStatus(500);
            return new ResponseResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(),"请检查输入格式");
        }
        if (i>0)
        {
            //                使用消息队列发送日志
            SendLogResult sendLogResult = new SendLogResult();
            sendLogResult.setUsername(refundConfirmParam.getRefundInfo());
            sendLogResult.setInfo("向退款ID:"+refundConfirmParam.getRefundId()+"修改状态为:"+3+"-公寓退款否决");
            amqpTemplate.convertAndSend(RabbitMQConfig.SEND_LOG_QUEUE,sendLogResult);
//                end消息队列发送日志

            return new ResponseResult<>(HttpStatus.OK.value(),"公寓退款否决",null);
        }
        else {
            httpServletResponse.setStatus(500);
            return new ResponseResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(),"请检查输入格式");
        }
    }

    /**
     * 搜索退款
     * @param pageNum
     * @param search
     * @return
     */
    @GetMapping(value = "/refund/apartment/list/{search}/{pageNum}")
    public ResponseResult<PageHelpDTO> userListSearch(@PathVariable int pageNum,@PathVariable String search){
        PageHelper.startPage(pageNum,5,"tb_refund.createTime DESC");
        PageInfo<ApartmentRefundList> pageInfo=new PageInfo<>(apartmentRefundListService.getRefundInfoListSearch(search));
        List<ApartmentRefundList> apartmentRefundLists = pageInfo.getList();
        PageHelpDTO pageHelpDTO=new PageHelpDTO();
        pageHelpDTO.setPages(pageInfo.getPages());
        pageHelpDTO.setTotal(pageInfo.getTotal());
        pageHelpDTO.setData(apartmentRefundLists);
        return new ResponseResult<>(200,"搜索退款信息",pageHelpDTO);
    }

}
